#! C:\Perl\bin\perl.exe

# Copyright (c) 2010, College Center for Library Automation

# See the included Copyright.txt file for full license

#
#
#

use strict;
use warnings;

use LWP::UserAgent;
use HTTP::Request;
use HTTP::Request::Common;
use Date::Calc qw(Add_Delta_YM Add_Delta_Days Today Delta_YMD);

use avsCommon;


dbsetup();

my ($content, $response, $request, $url);
my $session;

# create user agent, set cookies
my $ua = new LWP::UserAgent(keep_alive=>1);
$ua->cookie_jar( {} );

# access vendor site to get logged in
getLogin();

# loop through all the colleges
do {
	getStats ();
} while ($sthlogin->fetch);	

dbshutdown();

exit;

sub getLogin {

	#login
	$url="http://counterstats.greenwood.com/cgi-bin/stats.pl?user=$vendorusername&pass=$vendorpassword&Submit=Submit";

	$response = $ua->request(GET $url);
	if (!$response->is_success) {
		die "#error:$0: GET MENU $url : " . $response->status_line;
	};

	$content=$response->decoded_content;
#	print "$content\n";
	$content =~ /name=\"s\" value=\"(?<session>.*?)\"/s;
	$session = $+{session};
}

sub getStats {

	# setup dates to pass in url
	my @rundate = @enddate;
	my ($runyear, $runmonth, $runday) = @rundate;
	my $datasource;
	
	# run once for each year covered by beg and end date. typically beg and end month the same.
	do {
		# parameter for url
		my $yyyymm = sprintf('%04d%02d', $runyear, $runmonth);

		# how many months are we running for in this loop. typically one
		my ($deltayears, $deltamonths, $deltadays) = Delta_YMD(@begdate, @rundate);
		$deltamonths += $deltayears * 12;
		$deltamonths = 12 if $deltamonths > 12;
		
		# get sessions and searches report
		$url="http://counterstats.greenwood.com/cgi-bin/stats.pl?s=$session&action=dr1&yearmon=$yyyymm&num_months=$deltamonths&cust_id=$option1&format=html&submit=submit";

		$response = $ua->request(GET $url);
		if (!$response->is_success) {
			die "#error:$0: GET sessions and searches $url : " . $response->status_line;
		};

		$content=$response->decoded_content;

		# save contents for possible debugging
		savecontent('Greenwood', "searches$collegeid-$yyyymm", $content);
		
		# setup date ranges for captured data
		my @periods = ('','','','');	# ignore non date columns
		my ($tmpyear, $tmpmonth, $tmpday) = Add_Delta_YM($runyear, $runmonth, $runday, 0, -$deltamonths);	# find begining of months
		my $begperiod = myperiod($tmpyear, $tmpmonth);
		my $endperiod = myperiod($runyear, $runmonth);

		# used to decide what columns to capture
		my $begperiodndx;
		my $endperiodndx;

		my ($data, $dates);
	
		if (index($content, "No usage found for this period.") > -1) {
			print "#info: no data in file searches$collegeid-$yyyymm\n";
		}
		else {

			# find the data in the html
			$content =~ /Publisher<\/th>(?<dates>.*?)<\/tr>(?<data>.*?)<\/table>/s;
			$data = $+{data};
			$dates = $+{dates};

			$data =~ s/&nbsp;//g; # remove spaces/cr lf
			$data =~ s/<\/tr>/\n/gi; # add cr
			$data =~ s/<\/td>/~/gi; # add splitable character
			$data =~ s/<.*?>//gi; # remove html

			$begperiodndx = 4;
			$endperiodndx = 4;

			# save the date information
			while ($dates =~ /(?<month>\w+)-(?<year>\d{4})/g) {
				my $period = myperiod($+{year},$+{month});
				$begperiodndx++ if ($period lt $begperiod);
				$endperiodndx++ if ($period lt $endperiod);
				push @periods, $period;
			}

			my @sessions=(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
			my @searches=(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);

			foreach my $line (split(/\n/, $data)) {
				if ($line eq '~') {
					foreach my $i ($begperiodndx..$endperiodndx) {
						print "$vendorid $collegeid $periods[$i] $datasource $sessions[$i] $searches[$i]\n";
						$sthstats->execute($vendorid, $collegeid, $datasource, $periods[$i], $sessions[$i], 1)
							or die '#error: stats sessions failed';
						$sthstats->execute($vendorid, $collegeid, $datasource, $periods[$i], $searches[$i], 2)
							or die '#error: stats searches failed';

					};
					@sessions=(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
					@searches=(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
					next;
				}

				my @columns = split(/~/, $line);
				$datasource = $columns[0];
				if ($columns[3] eq 'Sessions run') {
					@sessions = @columns;
				}
				else {
					@searches = @columns;
				}
			}
		}
		
		#
		# now get ebooks stats
		#
		$url="http://counterstats.greenwood.com/cgi-bin/stats.pl?s=$session&action=br5&yearmon=$yyyymm&num_months=$deltamonths&cust_id=$option1&format=html&submit=submit";

		$response = $ua->request(GET $url);
		if (!$response->is_success) {
			die "#error:$0: GET MENU $url : " . $response->status_line;
		};

		$content=$response->decoded_content;

		# save contents for possible debugging
		savecontent('Greenwood', "ebooks$collegeid-$yyyymm", $content);

#		$content =~ /Publisher<\/th>(?<dates>.*?)<\/tr.*?<\/tr><tr>(?<data>.*?)<\/table>/s; # regex used to get all book titles
		$content =~ /Publisher<\/th>(?<dates>.*?)<\/tr><tr>(?<data>.*?)<\/tr>/s;	# only get total line
		$data = $+{data};
		$dates = $+{dates};

		$data =~ s/\x0A|\x0D//sg; # remove crs
		$data =~ s/<tr>/\n/sgi; # add cr
		$data =~ s/<\/td>/~/gi; # add splitable character
		$data =~ s/<.*?>|&nbsp;//gi; # remove html
		$data .= "\n";

		# grab the dates off of the header lines
		@periods = ('','','','','');	# ignore non date columns
		$begperiodndx = 5;
		$endperiodndx = 5;
		
		while ($dates =~ /(?<month>\w+)-(?<year>\d{4})/g) {
			my $period = myperiod($+{year},$+{month});
			$begperiodndx++ if ($period lt $begperiod);
			$endperiodndx++ if ($period lt $endperiod);
			push @periods, $period;
		}

		# read through each title see line 168 for regex limiting to total line
		foreach my $line (split(/\n/, $data)) {
			my @columns = split(/~/, $line);
			$datasource = "$columns[0] $columns[3]";	# tack isbn onto book title
			foreach my $i ($begperiodndx..$endperiodndx) {
				my $accesses = $columns[$i+1];
				print "$vendorid $collegeid $periods[$i] $datasource $accesses\n";
				$sthstats->execute($vendorid, $collegeid, $datasource, $periods[$i], $accesses, 3)
					or die '#error: estats execute failed';
			};
		}
		
		($runyear, $runmonth, $runday) = @rundate = Add_Delta_YM($runyear, $runmonth, $runday, -1, 0);	# retreat one year

	}	until ($runyear < $begyear || $runyear == $begyear && $runmonth < $begmonth);
}